package com.ruoyi.swla2.controller;

import cn.hutool.core.util.CoordinateUtil;
import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.swla2.domain.Terminals;
import com.ruoyi.swla2.model.ZoneJoinModel;
import com.ruoyi.swla2.service.ITerminalsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 大表档案Controller
 *
 * @author gdqt
 * @date 2023-06-16
 */
@RestController
@RequestMapping("/device/terminalsInfo")
public class TerminalsController extends BaseController {
    @Autowired
    private ITerminalsService terminalsService;

    /**
     * 查询大表档案列表
     */
    @PreAuthorize("@ss.hasPermi('device:terminalsInfo:list')")
    @GetMapping("/listAllTerminals")
    public AjaxResult listAllTerminals(Terminals terminals) {
        terminals.setTerminalMark(1L);
        List<Terminals> list = terminalsService.selectTerminalsList(terminals);
        list.forEach(t -> {
            // 使用定位控件定位的经纬度不需要转换坐标
            if (StrUtil.isNotEmpty(t.getLng()) && StrUtil.isNotEmpty(t.getLat()) && t.getLngLatTemp() == null) {
                CoordinateUtil.Coordinate coordinate = CoordinateUtil.bd09ToGcj02(Double.parseDouble(t.getLng()), Double.parseDouble(t.getLat()));
                t.setLng(String.valueOf(coordinate.getLng()));
                t.setLat(String.valueOf(coordinate.getLat()));
            }
        });
        return AjaxResult.success(list);
    }

    /**
     * 查询大表档案列表
     */
    @PreAuthorize("@ss.hasPermi('device:terminalsInfo:list')")
    @GetMapping("/list")
    public TableDataInfo list(Terminals terminals) {
        startPage();
        terminals.setTerminalMark(1L);
        List<Terminals> list = terminalsService.selectTerminalsList(terminals);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('device:terminalsInfo:list')")
    @GetMapping("/getUnbindTerminals")
    public TableDataInfo getUnbindTerminals(ZoneJoinModel model) {
        startPage();
        model.setTerminalMark(1);
        List<Terminals> list = terminalsService.getUnbindTerminals(model);
        return getDataTable(list);
    }

    /**
     * 导出大表档案列表
     */
    @PreAuthorize("@ss.hasPermi('device:terminalsInfo:export')")
    @Log(title = "大表档案", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Terminals terminals) {
        List<Terminals> list = terminalsService.selectTerminalsList(terminals);
        ExcelUtil<Terminals> util = new ExcelUtil<Terminals>(Terminals.class);
        util.exportExcel(response, list, "大表档案数据");
    }

    /**
     * 获取大表档案详细信息
     */
    @PreAuthorize("@ss.hasPermi('device:terminalsInfo:query')")
    @GetMapping(value = "/{ID}")
    public AjaxResult getInfo(@PathVariable("ID") Long ID) {
        return success(terminalsService.selectTerminalsByID(ID));
    }

    /**
     * 新增大表档案
     */
    @PreAuthorize("@ss.hasPermi('device:terminalsInfo:add')")
    @Log(title = "大表档案", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Terminals terminals) {
        terminals.setTenantId(this.getTenantId());
        return toAjax(terminalsService.insertTerminals(terminals));
    }

    /**
     * 修改大表档案
     */
    @PreAuthorize("@ss.hasPermi('device:terminalsInfo:edit')")
    @Log(title = "大表档案", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Terminals terminals) {
        return toAjax(terminalsService.updateTerminals(terminals));
    }

    /**
     * 删除大表档案
     */
    @PreAuthorize("@ss.hasPermi('device:terminalsInfo:remove')")
    @Log(title = "大表档案", businessType = BusinessType.DELETE)
    @DeleteMapping("/{IDs}")
    public AjaxResult remove(@PathVariable Long[] IDs) {
        return toAjax(terminalsService.deleteTerminalsByIDs(IDs));
    }
}
